import Mock from "mockjs";
let random = Mock.Random;
random.extend({
  taskStatus: function () {
    let status = [0, 1, 2]; // 0编辑中，1启用，2停用
    return this.pick(status);
  },
  dataType: function () {
    let types = ["int", "varchar", "boolean"];
    return this.pick(types);
  },
});
function paramToObj(url) {
  const search = url.split("?")[1];
  if (!search) {
    return {};
  }
  let params = {};
  search.split("&").forEach((item) => {
    item = item.split("=");
    let _key = item[0];
    let _value = item[1];
    params[_key] = _value;
  });
  return params;
}
// 任务列表
export const getTaskList = function (option) {
  console.log("option", option.url);
  console.log("type", option.type.toLowerCase());
  let { pageNum } =
    option.type.toLowerCase() === "post"
      ? JSON.parse(option.body)
      : paramToObj(option.url);
  const modelList = Mock.mock({
    code: 200,
    message: "ok",
    data: {
      total: 100,
      "list|10": [
        {
          "id|+1": 1,
          taskName: "admin", // 任务名称
          syncDataSource: "武器装备本体", // 同步数据源
          syncDataSourceId: 1, // 同步数据源id
          syncObject: "武器装备本体333", // 同步对象
          targetDataSource: "admin1", // 目标数据源
          targetModel: "targetModel", // 目标模型
          schedulingFrequency: "每周", // 调度频率
          lastExcuteTime: "2021-01-01: 00:00:00", // 最新执行时间
          status: random.taskStatus(), // 调度状态
          createTime: "2021-01-01: 00:00:00", // 创建时间
          createUserName: "章三", // 创建人
          description: "描述", // 任务说明
        },
      ],
      pageNum: pageNum,
      pageSize: 10,
    },
  });
  return modelList;
};

export const editTask = function (option) {
  return {
    code: 200,
    message: "ok",
  };
};
export const deleteTask = function (option) {
  return {
    code: 200,
    message: "ok",
  };
};
export const execTask = function (option) {
  return {
    code: 200,
    message: "ok",
  };
};
export const changeStatus = function (option) {
  return {
    code: 200,
    message: "ok",
  };
};
export const execAllTask = function (option) {
  return {
    code: 200,
    message: "ok",
  };
};

export const getAllSourceList = function (option) {
  return Mock.mock({
    code: 200,
    message: "ok",
    data: [
      {
        key: 1,
        value: "数仓",
      },
      {
        key: 2,
        value: "武器装备本体",
      },
      {
        key: 3,
        value: "武器装备本体11",
      },
    ],
  });
};

export const getSyncConstraintListAndSelectedList = function () {
  return {
    code: 200,
    message: "ok",
    data: {
      chooseList: [
        {
          mappingTableId: 1,
          name: "code1",
          remark: "code",
          status: 0, // 表类型 0 指 “表”
        },
        {
          mappingTableId: 2,
          name: "name1",
          remark: "姓名",
          status: 0,
        },
        {
          mappingTableId: 3,
          name: "age1",
          remark: "年龄",
          status: 0,
        },
      ],
      unChooseList: [
        {
          name: "code2",
          remark: "code",
          status: 0,
        },
        {
          name: "name2",
          remark: "姓名",
          status: 0,
        },
        {
          name: "age2",
          remark: "年龄",
          status: 0,
        },
      ],
    },
  };
};

export const getSyncSelectedList = function () {
  return {
    code: 200,
    message: "ok",
    data: {
      selectedList: [
        {
          id: 33,
          name: "age",
          remark: "年龄",
          type: "图",
          typeId: 2,
        },
      ],
    },
  };
};
export const getAllTypeList = function () {
  return {
    code: 200,
    message: "ok",
    data: [
      {
        id: 0,
        name: "全部",
      },
      {
        id: 1,
        name: "表",
      },
      {
        id: 2,
        name: "图",
      },
    ],
  };
};

export const saveSelectedList = function () {
  return {
    code: 200,
    message: "ok",
  };
};
export const getSourceList = function (option) {
  return Mock.mock({
    code: 200,
    message: "ok",
    data: [
      {
        mappingTableId: 1,
        syncObject: "@name",
        targetModelName: "@name",
        targetModelId: 1,
        "sourceList|3": [
          {
            sourceField: "@name",
            comment: "@cname",
            type: "@pick(['int','varchar','boolean'])",
          },
        ],
        "targetList|2": [
          {
            targetField: "@name",
            comment: "@cname",
            type: "@pick(['int','varchar','boolean'])",
          },
        ],
        "mapList|4": [
          {
            "mappingTableFieldId|+1": 1,
            "mappingTableId|+1": 10,
            sourceField: "@name",
            targetField: "@name",
            sourceComment: "@cname",
            targetComment: "@cname",
            sourceType: "@pick(['int','varchar','boolean'])",
            targetType: "@pick(['int','varchar','boolean'])",
          },
        ],
      },
      {
        mappingTableId: 2,
        syncObject: "@name",
        targetModelName: "@name",
        targetModelId: 2,
        "sourceList|3": [
          {
            sourceField: "@name",
            comment: "@cname",
            type: "@pick(['int','varchar','boolean'])",
          },
        ],
        "targetList|2": [
          {
            targetField: "@name",
            comment: "@cname",
            type: "@pick(['int','varchar','boolean'])",
          },
        ],
        "mapList|4": [
          {
            "mappingTableFieldId|+1": 1,
            "mappingTableId|+1": 10,
            sourceField: "@name",
            targetField: "@name",
            sourceComment: "@cname",
            targetComment: "@cname",
            sourceType: "@pick(['int','varchar','boolean'])",
            targetType: "@pick(['int','varchar','boolean'])",
          },
        ],
      },
    ],
  });
};

export const getTargetList = function () {
  return Mock.mock({
    code: 200,
    message: "ok",
    "data|10": [
      {
        "id|+1": 1,
        name: "@name",
      },
    ],
  });
};

export const delSourceData = function () {
  return {
    code: 200,
    message: "ok",
  };
};

export const getMapList = function () {
  return Mock.mock({
    code: 200,
    message: "ok",
    data: {
      "sourceList|5": [
        {
          sourceField: "@name",
          comment: "@cname",
          type: "@pick(['int','varchar','boolean'])",
        },
      ],
      "targetList|5": [
        {
          targetField: "@name",
          comment: "@cname",
          type: "@pick(['int','varchar','boolean'])",
        },
      ],
      "mapList|4": [
        {
          "mappingTableFieldId|+1": 1,
          "mappingTableId|+1": 10,
          sourceField: "@name",
          targetField: "@name",
          sourceComment: "@cname",
          targetComment: "@cname",
          sourceType: "@pick(['int','varchar','boolean'])",
          targetType: "@pick(['int','varchar','boolean'])",
        },
      ],
    },
  });
};

export const saveMapList = function () {
  return {
    code: 200,
    message: "ok",
  };
};

export const getDigitisingResults = function (option) {
  let { pageNum } =
    option.type.toLowerCase() === "post"
      ? JSON.parse(option.body)
      : paramToObj(option.url);
  return Mock.mock({
    code: 200,
    message: "ok",
    data: {
      list: [
        {
          taskResultId: 1,
          taskName: "@cname", // 任务名称
          syncDataSourceId: 1, // 同步数据源id
          syncDataSource: "@name", // 同步数据源
          syncDataObject: "@name", // 同步对象
          targetDataSourceId: 4, //目标数据源id
          targetDataSource: "@name", // 目标数据源
          targetModel: "@name", // 目标模型
          syncTypeValue: "全删全增", // 更新策略 1.全删全增,2.追加,3.更新
          taskResultStatus: 0, // 当前任务状态 0执行中，1执行成功，2执行失败
        },
        {
          taskResultId: 2,
          taskName: "@cname", // 任务名称
          syncDataSourceId: 2, // 同步数据源id
          syncDataSource: "@name", // 同步数据源
          syncDataObject: "@name", // 同步对象
          targetDataSourceId: 1, //目标数据源id
          targetDataSource: "@name", // 目标数据源
          targetModel: "@name", // 目标模型
          syncTypeValue: "全删全增", // 更新策略
          taskResultStatus: 1, // 当前任务状态
        },
        {
          taskResultId: 3,
          taskName: "@cname", // 任务名称
          syncDataSourceId: 3, // 同步数据源id
          syncDataSource: "@name", // 同步数据源
          syncDataObject: "@name", // 同步对象
          targetDataSourceId: 2, //目标数据源id
          targetDataSource: "@name", // 目标数据源
          targetModel: "@name", // 目标模型
          syncTypeValue: "全删全增", // 更新策略
          taskResultStatus: 2, // 当前任务状态
        },
        {
          taskResultId: 4,
          taskName: "@cname", // 任务名称
          syncDataSourceId: 4, // 同步数据源id
          syncDataSource: "@name", // 同步数据源
          syncDataObject: "@name", // 同步对象
          targetDataSourceId: 3, //目标数据源id
          targetDataSource: "@name", // 目标数据源
          targetModel: "@name", // 目标模型
          syncTypeValue: "全删全增", // 更新策略
          taskResultStatus: 1, // 当前任务状态
        },
      ],
      pageNum: pageNum,
      pageSize: 10,
      total: 20,
    },
  });
};

export const stopTask = function () {
  return {
    code: 200,
    message: "ok",
  };
};

export const getLogDetail = function (option) {
  let { pageNum } =
    option.type.toLowerCase() === "post"
      ? JSON.parse(option.body)
      : paramToObj(option.url);
  return Mock.mock({
    code: 200,
    message: "ok",
    data: {
      list: [
        {
          resultDetailId: 1,
          modelName: "@cname", // 模型名称
          taskName: "@cname", // 任务名称
          syncDataSource: "@name", // 同步数据源
          syncObject: "@name", // 同步对象
          targetDataSource: "@name", // 目标数据源
          targetModel: "@name", // 目标模型
          syncType: "全删全增", // 更新策略
          resultDetailStatus: 0, // 当前任务状态 1执行成功，2执行失败
          schedulingFrequency: "每周", // 执行频率
          startTime: "2021 01 01 11:00:00", // 开始时间
          endTime: "2022 01 01 11:00:00", // 开始时间
          addNum: 2, // 新增数量
          updateNum: 3, // 更新数量
          log: "mldkajflieajlfielaifjeligleaig", //日志
        },
        {
          resultDetailId: 2,
          modelName: "@cname", // 模型名称
          taskName: "@cname", // 任务名称
          syncDataSource: "@name", // 同步数据源
          syncObject: "@name", // 同步对象
          targetDataSource: "@name", // 目标数据源
          targetModel: "@name", // 目标模型
          syncType: "全删全增", // 更新策略
          resultDetailStatus: 1, // 当前任务状态
          schedulingFrequency: "每周", // 执行频率
          startTime: "2021 01 01 11:00:00", // 开始时间
          endTime: "2022 01 01 11:00:00", // 开始时间
          addNum: 2, // 新增数量
          updateNum: 3, // 更新数量
          log: "mldkajflieajlfielaifjeligleaig", //日志
        },
        {
          resultDetailId: 3,
          modelName: "@cname", // 模型名称
          taskName: "@cname", // 任务名称
          syncDataSource: "@name", // 同步数据源
          syncObject: "@name", // 同步对象
          targetDataSource: "@name", // 目标数据源
          targetModel: "@name", // 目标模型
          syncType: "全删全增", // 更新策略
          resultDetailStatus: 2, // 当前任务状态
          schedulingFrequency: "每周", // 执行频率
          startTime: "2021 01 01 11:00:00", // 开始时间
          endTime: "2022 01 01 11:00:00", // 开始时间
          addNum: 2, // 新增数量
          updateNum: 3, // 更新数量
          log: "mldkajflieajlfielaifjeligleaig", //日志
        },
        {
          resultDetailId: 4,
          modelName: "@cname", // 模型名称
          taskName: "@cname", // 任务名称
          syncDataSource: "@name", // 同步数据源
          syncObject: "@name", // 同步对象
          targetDataSource: "@name", // 目标数据源
          targetModel: "@name", // 目标模型
          syncType: "全删全增", // 更新策略
          resultDetailStatus: 3, // 当前任务状态
          schedulingFrequency: "每周", // 执行频率
          startTime: "2021 01 01 11:00:00", // 开始时间
          endTime: "2022 01 01 11:00:00", // 开始时间
          addNum: 2, // 新增数量
          updateNum: 3, // 更新数量
          log: "mldkajflieajlfielaifjeligleaig", //日志
        },
      ],
      pageNum: pageNum,
      pageSize: 10,
      total: 20,
    },
  });
};

export const getResultDetail = function (option) {
  return {
    code: 200,
    message: "ok",
    data: {
      resultDetailId: 17,

      modelName: "test_physical",

      startTime: "2022-02-17 15:17:00",

      endTime: "2022-02-17 15:17:20",

      logDetail: "执行成功",
    },
  };
};
//数据维护列表
export const getModelList = function (option) {
  let { pageNum } =
    option.type.toLowerCase() === "post"
      ? JSON.parse(option.body)
      : paramToObj(option.url);
  const modelList = Mock.mock({
    code: 200,
    message: "ok",
    data: {
      total: 100,
      "list|10": [
        {
          "modelId|+1": 1, //列表项唯一标识
          modelCode: "MCSLJMX02", //模型编码
          modelName: "人员信息模型", //模型名称
          themeName: "人员管理数据", //所属主题
          tableName: "表名", // 表名
          dataSourceId: 1,
          dataSourceName: "项目数据源",
          dataCount: 9000,
        },
      ],
      pageNum: pageNum,
      pageSize: 10,
    },
  });
  return modelList;
};
export const getSubjectList = function (option) {
  const subjectList = Mock.mock({
    code: 200,
    message: "ok",
    data: [
      {
        id: 1, //列表项唯一标识
        label: "XX体系业务数据", //主题名称
        children: [
          {
            //子集
            id: 4,
            label: "业务标准管理",
            children: [
              {
                id: 9,
                label: "申报标准",
                children: [
                  {
                    id: 11,
                    label: "XXXXXXXX申报",
                  },
                ],
              },
              {
                id: 10,
                label: "审批标准",
              },
            ],
          },
        ],
      },
    ],
  });
  return subjectList;
};

export const getDataMaintenanceDetail = function (option) {
  let { pageNum } =
    option.type.toLowerCase() === "post"
      ? JSON.parse(option.body)
      : paramToObj(option.url);
  return Mock.mock({
    code: 200,
    message: "ok",
    data: {
      modelName: "人员信息模型",
      fieldList: {
        id: "ID",
        username: "人员名称",
        sex: "人员性别",
        createTime: "创建时间",
        createUserName: "创建人",
        updateTime: "更新时间",
        updateUser: "更新人姓名",
        age: "年龄",
        like: "爱好",
        status: "生效",
      },
      "dataList|4": [
        {
          "id|+1": 1,
          username: "@cname",
          createUsername: "@cname",
          updateUser: "@cname",
          sex: "男",
          age: 10,
          createTime: "2021 01 02 11:00:00",
          updateTime: "2021 01 02 11:00:00",
          like: "足球1111111111111111111111111111",
          status: 0,
        },
      ],
      pageNum: pageNum,
      pageSize: 10,
      total: 20,
    },
  });
};

export const saveModelData = function (option) {
  return {
    code: 666,
    message: "ok",
    data: [
      {
        field: "username",
        logDetail: "用户名重复，请重新输入，巴拉巴拉巴拉巴拉",
      },
    ],
  };
};
export const exportModelData = function (option) {
  return {
    code: 200,
    message: "ok",
    data: null,
  };
};
export const exportTemplateOfModelData = function (option) {
  return {
    code: 200,
    message: "ok",
    data: null,
  };
};
export const uploadModelData = function (option) {
  return {
    code: 200,
    message: "ok",
    data: null,
  };
};
